package server;

/*
 * This file is part of VLCJ.
 *
 * VLCJ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VLCJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VLCJ.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright 2009, 2010 Caprica Software Limited.
 */

import java.net.ServerSocket;
import java.net.Socket;

/**
 * An example of how to stream a media file using RTP.
 * The client specifies an MRL of <code>rtp://@127.0.0.1:5555</code>
 * Parametros de entrada del servidor: serv [-s <address>] [-p <port>]
 */
public class StreamRtp {

    private int port;
    private String address;
    private boolean flag;
    private ServerSocket server_socket;

    public StreamRtp(String[] args) {
        String parametro;
        port = 8080;
        address = "127.0.0.1";
        flag = true;
        if ((args.length % 2) != 0) {
            flag = false;
        } else {
            try {
                for (int i = 0; i < args.length; i++) {
                    parametro = args[i];
                    i = i + 1;
                    if (parametro.equals("-s")) {
                        address = args[i];
                    } else if (parametro.equals("-p")) {
                        port = Integer.parseInt(args[i]);
                    } else {
                        flag = false;
                        break;
                    }
                }
                server_socket = new ServerSocket(port + 1);
            } catch (NumberFormatException e1) {
                flag = false;
            } catch (Exception e2) {
                System.out.println("Error irrecuperable durante el inicio del servidor.");
                System.exit(-1);
            }
        }
    }

    public void server () {
        Socket cliente;
        String options;
        hiloComando hcomandos;
        int i = 0;
        if (flag) {
            System.out.println("Servidor activo en la direccion " + address + " y puerto " + port + ".");
            while(true) {
                try {
                    cliente = server_socket.accept();
                    options = formatRtpStream(address, port + (i * 1000));
                    hcomandos = new hiloComando(options, cliente, i);
		    i = i + 1;
                } catch (Exception e) {
                    System.out.println("Error irrecuperable durante la conexion con el cliente." + e.getMessage());
                }
            }
        } else {
            System.out.println("Usage: serv [-s address] [-p port]");
        }
    }

    private static String formatRtpStream(String serverAddress, int serverPort) {
        StringBuilder sb = new StringBuilder(60);
        sb.append(":sout=#rtp{dst=");
        //sb.append(":sout=#duplicate{dst=display,dst=rtp{dst=");
        sb.append(serverAddress);
        sb.append(",port=");
        sb.append(serverPort);
        sb.append(",mux=ts}");
        return sb.toString();
    }

    private static String formatHttpStream(String serverAddress, int serverPort) {
        StringBuilder sb = new StringBuilder(60);
        sb.append(":sout=#duplicate{dst=std{access=http,mux=ts,");
        sb.append("dst=");
        sb.append(serverAddress);
        sb.append(':');
        sb.append(serverPort);
        sb.append("}}");
        return sb.toString();
    }


    public static void main(String[] args) throws Exception {
        StreamRtp stream = new StreamRtp(args);
        stream.server();
    }
}
